﻿using Common;
using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.SingleDelivery
{
    public partial class SingleDeliveryPrint : Form
    {
        GridppReport Report = new GridppReport();
        NursingForm formService = new NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string Id;
        ElectronicSignature es;
        public SingleDeliveryPrint(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            Id = id;
            es = new ElectronicSignature(this.LoginUser);
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\分娩记录单.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordOrderByTime(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.Parturitions, 1);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(this.Id))
                    list = list.Where(p => p.Id.Equals(Convert.ToInt32(this.Id))).ToList();
                CreateDataTableHead(dt, list);
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    Parturition fd = XmlUtil.XMLToObject<Parturition>(record.NursingFormData, Encoding.UTF8);
                    string image1 = string.Empty;
                    string qianming1 = fd.JiLuZhe;
                    if (!string.IsNullOrEmpty(qianming1))
                    {
                        image1 = es.GetElectricSignBase64(record.AreaId, qianming1);
                    }
                    string hebingzheng = GetRshbzStr(fd);
                    string bingfazheng = GetRsbfzStr(fd);
                    dt.Rows.Add(
                        PatientInfo.PATIENT_NAME,
                        PatientInfo.BED_NO,
                        PatientInfo.PATIENT_ID,
                        fd.ZhenSuoKaiShi,    //阵缩开始
                        fd.TaiMoPoLie_ShiJian,    //胎膜_时间
                        fd.TaiMoPoLie_Text,   //胎膜_下拉选择
                        fd.YangShui,   //羊水
                        fd.GongKouKaiQuan,   //宫口开全
                        fd.DiYiChanChen_Shi,   //第一产程_时
                        fd.DiYiChanChen_Fen,   //第一产程_分
                        fd.FenHouYangShui,   //分后羊水
                        fd.TaiErMianChu_One,   // 胎儿娩出_1
                        fd.TaiErMianChu_Two,   // 胎儿娩出_2
                        fd.DiErChanChen_Shi,   //第二产程_时
                        fd.DiErChanChen_Fen,   // 第二产程_分
                        fd.TaiPanMianChu,   //胎盘娩出
                        fd.DiSanChanChen_Shi,   //第三产程_时
                        fd.DiSanChanChen_Fen,   //第三产程_分
                        fd.ZongChanCheng_Shi,     //总产程_时
                        fd.ZongChanCheng_Fen,   //总产程_分
                        fd.FenMianFangShi,     //分娩方式
                        fd.FenMianFangShi2,     //分娩方式
                        fd.FenMianFangShi_Text,    //分娩方式_文本框
                        fd.TaiChanWei,  //胎产位
                        fd.TaiPanFangShi_1,   // 胎盘方式_1
                        fd.TaiPanFangShi_2,   // 胎盘方式_2
                        fd.TiJi,   //体积
                        fd.ZhongLiang,   // 重量
                        fd.TaiPanQingKuang,   //胎盘情况
                        fd.TaiMo,   //胎膜
                        fd.QiDaiChang,   //脐带长
                        fd.RaoJing_Zhou,     //绕颈_周
                        fd.RaoJing_XiaLaXuanXiang,   //绕颈_下拉框选项
                        fd.TaiPanMianChuShiChuXue,    //胎盘娩出时出血
                        fd.ChanHouSanShiChuXue,  //产后30出血
                        fd.SiWu,   //45出血
                        fd.LiuShi,     //60出血
                        fd.JiuShi,   //90出血
                        fd.YiBaiEr,    //120出血
                        fd.ChanHouLiangXiaoShiZongChuXueLiang,  //产后两小时总出血量
                        fd.ChanHouXueYa_Di,   //产后血压_低
                        fd.ChanHouXueYa_Gao,     //产后血压_高
                        fd.ChanShiYongYao_CuiChanSuZhuShe_Liang,   //产时用药_催产素注射_量
                        fd.ChanShiYongYao_BuWei,    //产时用药_部位
                        fd.ChanShiYongYao_QiTa,  //产时用药_其他
                        fd.XinShengEr_XingBie,   //新生儿_性别
                        fd.XinShengEr_TiZhong,   //新生儿_体重
                        fd.XinShengEr_ShenChang,     //新生儿_身长
                        fd.APgar_PingFen,   //APgar评分
                        string.IsNullOrEmpty(fd.XinShengEr_XingBie2) ? "/" : fd.XinShengEr_XingBie2,   //新生儿2_性别
                        string.IsNullOrEmpty(fd.XinShengEr_TiZhong2) ? "/" : fd.XinShengEr_TiZhong2,   //新生儿2_体重
                        string.IsNullOrEmpty(fd.XinShengEr_ShenChang2) ? "/" : fd.XinShengEr_ShenChang2,     //新生儿2_身长
                        string.IsNullOrEmpty(fd.APgar_PingFen2) ? "/" : fd.APgar_PingFen2,   //APgar评分2
                        fd.HuiYinPoLie,    //会阴破裂
                        fd.NeiFeng_Zhen,  //内缝_针
                        fd.NeiFeng_Xian,   //内缝_线
                        fd.WaiFeng_Zhen,   //外缝_针
                        fd.WaiFeng_Xian,     //外缝_线
                        fd.YinChunPoLie,   //阴唇破裂
                        fd.YinDaoPoLie,    //阴道破裂
                        fd.GongJingLieKai,  //宫颈裂开
                        fd.XiuBuZhe,   //修补者
                        fd.HuiYinFengHeHouGangJian,   //会阴缝合后肛检
                        fd.HuiYinFengHeHouGangJian_Text,    //会阴缝合后肛检_Text
                        fd.ZhenDuan_Tai,  //诊断_胎
                        fd.ZhenDuan_Chan,   //诊断_产
                        fd.ZhenDuan_Zhou,   //诊断_周
                        fd.ZhenDuan_Mian,    //诊断_娩
                        fd.TaiFangWei,  //胎方位
                        fd.RenShenBingFaZheng,   //妊娠并发症
                        fd.RenShenBingFaZheng_Text,   //妊娠并发症_text
                        bingfazheng,//并发症拼接后
                        fd.RenShenHeBingZheng,    //妊娠合并症
                        fd.RenShenHeBingZheng_Text,  //妊娠合并症_text
                        hebingzheng,//合并症拼接后
                        fd.YingErPiFuZaoJieChuZaoXiShunFenZhong,   //婴儿皮肤早接触、早吸吮产_分钟
                        fd.YingErPiFuZaoJieChuZaoXiShunFenZhong_ChiXuFenZhong,   //婴儿皮肤早接触、早吸吮产_持续分数
                        fd.YingErPiFuZaoJieChuZaoXiShunFenZhong_XiShunQingKuang,    //婴儿皮肤早接触、早吸吮产_吸吮情况
                        fd.WeiXingPiFuZaoJieChuZoXiShunYuanYin,  //未行皮肤早接触、早吸吮原因
                        fd.BeiZhu,   //备注
                        fd.ChanFuLiKaiChnFangShiJian,   //产妇离开产房时间
                        fd.JieShengZhe,    //接生者
                        fd.JieShengZhe2,    //接生者2
                        fd.JieShengZhe3,    //接生者3
                        fd.JiLuZhe   //记录者
                    );
                    if (!string.IsNullOrEmpty(image1) && dt.Columns.Contains("ElectricSignBase64"))
                    {
                        dt.Rows[0]["ElectricSignBase64"] = image1;
                    }
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt, null);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }
        /// <summary>
        /// 加载列头
        /// </summary>
        /// <param name="dt"></param>
        private void CreateDataTableHead(DataTable dt, List<NursingFormRecord> list)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("ZhenSuoKaiShi", typeof(string));
            dt.Columns.Add("TaiMoPoLie_ShiJian", typeof(string));
            dt.Columns.Add("TaiMoPoLie_Text", typeof(string));
            dt.Columns.Add("YangShui", typeof(string));
            dt.Columns.Add("GongKouKaiQuan", typeof(string));
            dt.Columns.Add("DiYiChanChen_Shi", typeof(string));
            dt.Columns.Add("DiYiChanChen_Fen", typeof(string));
            dt.Columns.Add("FenHouYangShui", typeof(string));
            dt.Columns.Add("TaiErMianChu_One", typeof(string));
            dt.Columns.Add("TaiErMianChu_Two", typeof(string));
            dt.Columns.Add("DiErChanChen_Shi", typeof(string));
            dt.Columns.Add("DiErChanChen_Fen", typeof(string));
            dt.Columns.Add("TaiPanMianChu", typeof(string));
            dt.Columns.Add("DiSanChanChen_Shi", typeof(string));
            dt.Columns.Add("DiSanChanChen_Fen", typeof(string));
            dt.Columns.Add("ZongChanCheng_Shi", typeof(string));
            dt.Columns.Add("ZongChanCheng_Fen", typeof(string));
            dt.Columns.Add("FenMianFangShi", typeof(string));
            dt.Columns.Add("FenMianFangShi2", typeof(string));
            dt.Columns.Add("FenMianFangShi_Text", typeof(string));
            dt.Columns.Add("TaiChanWei", typeof(string));
            dt.Columns.Add("TaiPanFangShi_1", typeof(string));
            dt.Columns.Add("TaiPanFangShi_2", typeof(string));
            dt.Columns.Add("TiJi", typeof(string));
            dt.Columns.Add("ZhongLiang", typeof(string));
            dt.Columns.Add("TaiPanQingKuang", typeof(string));
            dt.Columns.Add("TaiMo", typeof(string));
            dt.Columns.Add("QiDaiChang", typeof(string));
            dt.Columns.Add("RaoJing_Zhou", typeof(string));
            dt.Columns.Add("RaoJing_XiaLaXuanXiang", typeof(string));
            dt.Columns.Add("TaiPanMianChuShiChuXue", typeof(string));
            dt.Columns.Add("ChanHouSanShiChuXue", typeof(string));
            dt.Columns.Add("SiWu", typeof(string));
            dt.Columns.Add("LiuShi", typeof(string));
            dt.Columns.Add("JiuShi", typeof(string));
            dt.Columns.Add("YiBaiEr", typeof(string));
            dt.Columns.Add("ChanHouLiangXiaoShiZongChuXueLiang", typeof(string));
            dt.Columns.Add("ChanHouXueYa_Di", typeof(string));
            dt.Columns.Add("ChanHouXueYa_Gao", typeof(string));
            dt.Columns.Add("ChanShiYongYao_CuiChanSuZhuShe_Liang", typeof(string));
            dt.Columns.Add("ChanShiYongYao_BuWei", typeof(string));
            dt.Columns.Add("ChanShiYongYao_QiTa", typeof(string));
            dt.Columns.Add("XinShengEr_XingBie", typeof(string));
            dt.Columns.Add("XinShengEr_TiZhong", typeof(string));
            dt.Columns.Add("XinShengEr_ShenChang", typeof(string));
            dt.Columns.Add("APgar_PingFen", typeof(string));
            dt.Columns.Add("XinShengEr_XingBie2", typeof(string));
            dt.Columns.Add("XinShengEr_TiZhong2", typeof(string));
            dt.Columns.Add("XinShengEr_ShenChang2", typeof(string));
            dt.Columns.Add("APgar_PingFen2", typeof(string));
            dt.Columns.Add("HuiYinPoLie", typeof(string));
            dt.Columns.Add("NeiFeng_Zhen", typeof(string));
            dt.Columns.Add("NeiFeng_Xian", typeof(string));
            dt.Columns.Add("WaiFeng_Zhen", typeof(string));
            dt.Columns.Add("WaiFeng_Xian", typeof(string));
            dt.Columns.Add("YinChunPoLie", typeof(string));
            dt.Columns.Add("YinDaoPoLie", typeof(string));
            dt.Columns.Add("GongJingLieKai", typeof(string));
            dt.Columns.Add("XiuBuZhe", typeof(string));
            dt.Columns.Add("HuiYinFengHeHouGangJian", typeof(string));
            dt.Columns.Add("HuiYinFengHeHouGangJian_Text", typeof(string));
            dt.Columns.Add("ZhenDuan_Tai", typeof(string));
            dt.Columns.Add("ZhenDuan_Chan", typeof(string));
            dt.Columns.Add("ZhenDuan_Zhou", typeof(string));
            dt.Columns.Add("ZhenDuan_Mian", typeof(string));
            dt.Columns.Add("TaiFangWei", typeof(string));
            dt.Columns.Add("RenShenBingFaZheng", typeof(string));
            dt.Columns.Add("RenShenBingFaZheng_Text", typeof(string));
            dt.Columns.Add("RenShenBingFaZhengStr", typeof(string));//New
            dt.Columns.Add("RenShenHeBingZheng", typeof(string));
            dt.Columns.Add("RenShenHeBingZheng_Text", typeof(string));//New
            dt.Columns.Add("RenShenHeBingZhengStr", typeof(string));
            dt.Columns.Add("YingErPiFuZaoJieChuZaoXiShunFenZhong", typeof(string));
            dt.Columns.Add("YingErPiFuZaoJieChuZaoXiShunFenZhong_ChiXuFenZhong", typeof(string));
            dt.Columns.Add("YingErPiFuZaoJieChuZaoXiShunFenZhong_XiShunQingKuang", typeof(string));
            dt.Columns.Add("WeiXingPiFuZaoJieChuZoXiShunYuanYin", typeof(string));
            dt.Columns.Add("BeiZhu", typeof(string));
            dt.Columns.Add("ChanFuLiKaiChnFangShiJian", typeof(string));
            dt.Columns.Add("JieShengZhe", typeof(string));
            dt.Columns.Add("JieShengZhe2", typeof(string));
            dt.Columns.Add("JieShengZhe3", typeof(string));
            dt.Columns.Add("JiLuZhe", typeof(string));
            if (list == null) return;
            NursingFormRecord record = list[0];
            Parturition fd = XmlUtil.XMLToObject<Parturition>(record.NursingFormData, Encoding.UTF8);
            //判断是否为空，不为空则添加列
            string qianming1 = fd.JiLuZhe;
            if (!string.IsNullOrEmpty(qianming1))
            {
                dt.Columns.Add("ElectricSignBase64", typeof(string));//电签
            }
        }

        private void SingleDeliveryPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }

        /// <summary>
        /// 获取并发症文字
        /// </summary>
        /// <param name="ps"></param>
        /// <returns></returns>
        private string GetRsbfzStr(Parturition ps)
        {
            string bfz = string.Empty;
            if (string.IsNullOrEmpty(ps.RenShenBingFaZheng)) return bfz;
            if (ps.RenShenBingFaZheng.Split('#')[0].Equals("1"))
                bfz += "流产、";
            if (ps.RenShenBingFaZheng.Split('#')[1].Equals("1"))
                bfz += "早产、";
            if (ps.RenShenBingFaZheng.Split('#')[2].Equals("1"))
                bfz += "过期妊娠、";
            if (ps.RenShenBingFaZheng.Split('#')[3].Equals("1"))
                bfz += "异位妊娠、";
            if (ps.RenShenBingFaZheng.Split('#')[4].Equals("1"))
                bfz += "妊娠期高血压疾病、";
            if (ps.RenShenBingFaZheng.Split('#')[5].Equals("1"))
                bfz += "前置胎盘、";
            if (ps.RenShenBingFaZheng.Split('#')[6].Equals("1"))
                bfz += "胎盘早剥、";
            if (ps.RenShenBingFaZheng.Split('#')[7].Equals("1"))
                bfz += "妊娠剧吐、";
            if (ps.RenShenBingFaZheng.Split('#')[8].Equals("1"))
                bfz += "多胎妊娠、";
            if (ps.RenShenBingFaZheng.Split('#')[9].Equals("1"))
                bfz += "羊水过多、";
            if (ps.RenShenBingFaZheng.Split('#')[10].Equals("1"))
                bfz += "羊水过少、";
            if (ps.RenShenBingFaZheng.Split('#')[11].Equals("1"))
                bfz += "母儿血型不合、";
            if (ps.RenShenBingFaZheng.Split('#')[12].Equals("1"))
                bfz += "胎儿生长受限、";
            if (ps.RenShenBingFaZheng.Split('#')[13].Equals("1"))
                bfz += "死胎、";
            if (ps.RenShenBingFaZheng.Split('#')[14].Equals("1"))
                bfz += "其他严重内外科疾病、";

            if (ps.RenShenBingFaZheng.Split('#').Length >= 17 && ps.RenShenBingFaZheng.Split('#')[16].Equals("1"))
                bfz += "胎膜早破、";
            if (ps.RenShenBingFaZheng.Split('#').Length >= 18 && ps.RenShenBingFaZheng.Split('#')[17].Equals("1"))
                bfz += "脐带绕颈、";

            if (ps.RenShenBingFaZheng.Split('#').Length >= 16 && ps.RenShenBingFaZheng.Split('#')[15].Equals("1"))
                bfz += ps.RenShenBingFaZheng_Text + "、";
            if (bfz.Length > 0)
                bfz = bfz.Substring(0, bfz.Length - 1);
            return bfz;
        }

        /// <summary>
        /// 获取合并症文字
        /// </summary>
        /// <param name="ps"></param>
        /// <returns></returns>
        private static string GetRshbzStr(Parturition ps)
        {
            string hbz = string.Empty;
            if (string.IsNullOrEmpty(ps.RenShenHeBingZheng)) return hbz;
            if (ps.RenShenHeBingZheng.Split('#')[0].Equals("1"))
                hbz += "无、";
            if (ps.RenShenHeBingZheng.Split('#')[1].Equals("1"))
                hbz += "贫血、";
            if (ps.RenShenHeBingZheng.Split('#')[2].Equals("1"))
                hbz += "心脏病、";
            if (ps.RenShenHeBingZheng.Split('#')[3].Equals("1"))
                hbz += "肾脏疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[4].Equals("1"))
                hbz += "血液系统疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[5].Equals("1"))
                hbz += "肝炎或肝损害、";
            if (ps.RenShenHeBingZheng.Split('#')[6].Equals("1"))
                hbz += "活动性肺结核、";
            if (ps.RenShenHeBingZheng.Split('#')[7].Equals("1"))
                hbz += "糖尿病、";
            if (ps.RenShenHeBingZheng.Split('#')[8].Equals("1"))
                hbz += "甲状腺功能亢进症、";
            if (ps.RenShenHeBingZheng.Split('#')[9].Equals("1"))
                hbz += "精神病或神经系统疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[10].Equals("1"))
                hbz += "妊娠合并免疫系统疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[11].Equals("1"))
                hbz += "卵巢或子宫肿瘤、";
            if (ps.RenShenHeBingZheng.Split('#')[12].Equals("1"))
                hbz += "孕期感染性疾病、";
            if (ps.RenShenHeBingZheng.Split('#')[13].Equals("1"))
                hbz += "传播性疾病(含艾滋病)、";
            if (ps.RenShenHeBingZheng.Split('#')[14].Equals("1"))
                hbz += "妊娠合并心律失常、";
            if (ps.RenShenHeBingZheng.Split('#')[15].Equals("1"))
                hbz += "妊娠合并血小板减少性紫癜、";
            if (ps.RenShenHeBingZheng.Split('#')[16].Equals("1"))
                hbz += "感染性疾病合并妊娠、";
            if (ps.RenShenHeBingZheng.Split('#')[17].Equals("1"))
                hbz += "妊娠合并生殖器畸形、";
            if (ps.RenShenHeBingZheng.Split('#')[18].Equals("1"))
                hbz += "其他严重内外科疾病、";
            if (ps.RenShenHeBingZheng.Split('#').Length >= 20 && ps.RenShenHeBingZheng.Split('#')[19].Equals("1"))
                hbz += ps.RenShenHeBingZheng_Text + "、";
            if (hbz.Length > 0)
                hbz = hbz.Substring(0, hbz.Length - 1);
            return hbz;
        }
    }
}
